package ipmss.ui.schedule;

import ipmss.entity.planning.Project;
import ipmss.entity.planning.Task;
import ipmss.planning.ProjectProvider;
import ipmss.planning.ScheduleInvoker;
import ipmss.planning.commands.AddTaskCommand;
import ipmss.planning.commands.ScheduleCommand;
import ipmss.services.common.UserContext;
import ipmss.ui.authorities.UsersManagementViewBean;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.context.annotation.Scope;

// TODO: Auto-generated Javadoc
/**
 * The Class NewTaskViewBean.
 */
@Named
@Scope("session")
public class NewTaskViewBean
{

    /** The Constant logger. */
    private static final Logger logger = Logger.getLogger(NewTaskViewBean.class.getName());

    /** The schedule invoker. */
    @Inject
    private ScheduleInvoker scheduleInvoker;
    
    /** The final hour per day. */
    private static int finalHourPerDay = 8;
    
    
    /**
     * The Class ProjectS.
     */
    public static class ProjectS
    {
	
	/** The name. */
	private String name;
	
	/** The id. */
	private int id;

	/**
	 * Gets the name.
	 *
	 * @return the name
	 */
	public String getName()
	{
	    return name;
	}

	/**
	 * Sets the name.
	 *
	 * @param name the new name
	 */
	public void setName(String name)
	{
	    this.name = name;
	}

	/**
	 * Gets the id.
	 *
	 * @return the id
	 */
	public int getId()
	{
	    return id;
	}

	/**
	 * Sets the id.
	 *
	 * @param id the new id
	 */
	public void setId(int id)
	{
	    this.id = id;
	}
    }
    
    
    /**
     * The Class ProjectS.
     */
    public static class TaskS
    {
	
	/** The name. */
	private String name;
	
	/** The id. */
	private int id;

	/**
	 * Gets the name.
	 *
	 * @return the name
	 */
	public String getName()
	{
	    return name;
	}

	/**
	 * Sets the name.
	 *
	 * @param name the new name
	 */
	public void setName(String name)
	{
	    this.name = name;
	}

	/**
	 * Gets the id.
	 *
	 * @return the id
	 */
	public int getId()
	{
	    return id;
	}

	/**
	 * Sets the id.
	 *
	 * @param id the new id
	 */
	public void setId(int id)
	{
	    this.id = id;
	}
    }

    /** The project provider. */
    @Inject
    private ProjectProvider projectProvider;
    
    /** The user context. */
    @Inject
    private UserContext userContext;
    
    /** The name. */
    private String name;
    
    /** The realization effort. */
    private int realizationEffort = 0;
    
    /** The estimated effort. */
    private int estimatedEffort = 0;
    
    /** The duration. */
    private int duration = 8;
    
    /** The start date. */
    private Date startDate;
    
    /** The end date. */
    private Date endDate;
    
    /** The percetage complete. */
    private int percetageComplete = 0;
    
    /** The project. */
    private int project;
    
    /** The projects. */
    private List<ProjectS> projects;

    /** The project id. */
    private int projectId;
    
    private List<TaskS> tasks;
    
    private int taskId;
    
    private int task;


    /**
     * Gets the percetage complete.
     *
     * @return the percetage complete
     */
    public int getPercetageComplete()
    {
	return percetageComplete;
    }

    /**
     * Sets the percetage complete.
     *
     * @param percetageComplete the new percetage complete
     */
    public void setPercetageComplete(int percetageComplete)
    {
	this.percetageComplete = percetageComplete;
    }

    /**
     * Gets the end date.
     *
     * @return the end date
     */
    public Date getEndDate()
    {
	return endDate;
    }

    /**
     * Sets the end date.
     *
     * @param endDate the new end date
     */
    public void setEndDate(Date endDate)
    {
	this.endDate = endDate;
    }

    /**
     * Gets the start date.
     *
     * @return the start date
     */
    public Date getStartDate()
    {
	return startDate;
    }

    /**
     * Sets the start date.
     *
     * @param startDate the new start date
     */
    public void setStartDate(Date startDate)
    {
	this.startDate = startDate;
    }

    /**
     * Gets the duration.
     *
     * @return the duration
     */
    public int getDuration()
    {
	return duration;
    }

    /**
     * Sets the duration.
     *
     * @param duration the new duration
     */
    public void setDuration(int duration)
    {
	this.duration = duration;
    }

    /**
     * Gets the estimated effort.
     *
     * @return the estimated effort
     */
    public int getEstimatedEffort()
    {
	return estimatedEffort;
    }

    /**
     * Sets the estimated effort.
     *
     * @param estimatedEffort the new estimated effort
     */
    public void setEstimatedEffort(int estimatedEffort)
    {
	this.estimatedEffort = estimatedEffort;
    }

    /**
     * Gets the realization effort.
     *
     * @return the realization effort
     */
    public int getRealizationEffort()
    {
	return realizationEffort;
    }

    /**
     * Sets the realization effort.
     *
     * @param realizationEffort the new realization effort
     */
    public void setRealizationEffort(int realizationEffort)
    {
	this.realizationEffort = realizationEffort;
    }

    /**
     * Gets the name.
     *
     * @return the name
     */
    public String getName()
    {
	return name;
    }

    /**
     * Sets the name.
     *
     * @param name the new name
     */
    public void setName(String name)
    {
	this.name = name;
    }

    /**
     * Creates the task.
     */
    public void createTask()
    {

	logger.log(Level.INFO, "create task");
	Project pr = projectProvider.getProjectById(project);
	Task added  =  new Task();
	
	if(getName() != null && getName() != "")
	{
	added.setName(getName());
	Calendar c =  Calendar.getInstance();
	c.setTime(getStartDate());
	added.setStartDate(c);
	c = Calendar.getInstance();
	c.getTime().setTime(c.getTimeInMillis() + getDuration() * (60*60*1000));
	added.setEndDate(c);
	added.setDuration(getDuration());
	added.setEstimatedEffort(getEstimatedEffort());
	added.setPercetageComplete(getPercetageComplete());
	
	if(task == 0)
	{
	    ScheduleCommand command = new AddTaskCommand(pr.getSchedule(), added);
	    scheduleInvoker.execute(command);
	
	}else
	{
	  
	    ScheduleCommand command = new AddTaskCommand(  findTaskMy(pr.getSchedule()), added);
	    
	    scheduleInvoker.execute(command);
	}
	// temporary
	projectProvider.saveAllProjcts();
	}

    }

    /**
     * Gets the project id.
     *
     * @return the project id
     */
    public int getProjectId()
    {
	return projectId;
    }

    /**
     * Sets the project id.
     *
     * @param projectId the new project id
     */
    public void setProjectId(int projectId)
    {
	this.projectId = projectId;
    }

    /**
     * Gets the project.
     *
     * @return the project
     */
    public int getProject()
    {
	return project;
    }

    /**
     * Sets the project.
     *
     * @param project the new project
     */
    public void setProject(int project)
    {
	this.project = project;
    }

    /**
     * Gets the projects.
     *
     * @return the projects
     */
    public List<ProjectS> getProjects()
    {
	List<Project> projects = userContext.getProjects();
	this.projects = new ArrayList<ProjectS>();
	for (Project p : projects)
	{
	    ProjectS ps = new ProjectS();
	    ps.setId(p.getId());
	    ps.setName(p.getName());
	    this.projects.add(ps);
	}

	return this.projects;
    }
    
   

    /**
     * Sets the projects.
     *
     * @param projects the new projects
     */
    public void setProjects(List<ProjectS> projects)
    {
	this.projects = projects;
    }

    /** temporary **/
    public List<TaskS> getTasks()
    {
	
	Project pr = projectProvider.getProjectById(project);
	 tasks = new ArrayList<TaskS> ();
	if(pr != null)
	    setChildTask(pr.getSchedule(), tasks);
	return tasks;
    }
    
    private Task findTaskMy()
    {
	Project pr = projectProvider.getProjectById(project);
	
	return findTaskMy(pr.getSchedule());
	
    }
    
    private Task findTaskMy(Task t)
    {
	if(t.getId() == task)
	    return t;
	else if(t.getTasks().size() >0)
	{
	    for(Task cT :  t.getTasks())
	    {
		Task f = findTaskMy(cT);
		if(f != null)
		{
		    return f;
		}
	    }
	}
	return null;
	
    }
    
    private void setChildTask(Task task,List<TaskS> tasks)
    {
	if (task.getTasks() != null)
	{
	    for (Task t : task.getTasks())
	    {
		TaskS ts = new TaskS();
		ts.setId(t.getId());
		ts.setName(t.getName());
		tasks.add(ts);
		setChildTask(t, tasks);
	    }
	}
    }

    public void setTasks(List<TaskS> tasks)
    {
	this.tasks = tasks;
    }

    public int getTaskId()
    {
	return taskId;
    }

    public void setTaskId(int taskId)
    {
	this.taskId = taskId;
    }

    public int getTask()
    {
	return task;
    }

    public void setTask(int task)
    {
	this.task = task;
    }

}
